package com.example.viewtest.gesture_12;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.example.viewtest.R;
import com.example.viewtest.Utils;

/**
 * 接力性多点触控
 */
public class RelayGestureView extends View {

    private Bitmap bitmap;
    private Paint paint;

    private float locationX;
    private float locationY;

    private float downX;
    private float downY;
    private float originX;
    private float originY;

    private int curActionIndex;
    private int pointerId;

    public RelayGestureView(Context context) {
        this(context, null);
    }

    public RelayGestureView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RelayGestureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        bitmap = Utils.getBitmap(getResources(), R.mipmap.head, (int) Utils.db2px(200));
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int masked = event.getActionMasked();
        switch (masked) {
            case MotionEvent.ACTION_DOWN:
                Log.e("onTouchEvent", "ACTION_DOWN: ");
                //保存第一次按下手指的id，index会变动id不会改变
                pointerId = event.getPointerId(0);
                downX = event.getX();
                downY = event.getY();
                originX = locationX;
                originY = locationY;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                curActionIndex = event.getActionIndex();
                downX = event.getX(curActionIndex);
                downY = event.getY(curActionIndex);
                originX = locationX;
                originY = locationY;
                break;
            case MotionEvent.ACTION_POINTER_UP:
                /*
                获取当前抬起的id，判断是否是第一个按下的手指
                如果是第一次按下的手指抬起，那通过index：0获取的坐标就是抬起手指的坐标，
                所以要取其他index的坐标才是当前还按着的手指坐标
                如果不是那直接取0
                 */
                curActionIndex = event.getPointerId(event.getActionIndex()) == pointerId ? event.getPointerCount() - 1 : 0;
                downX = event.getX(curActionIndex);
                downY = event.getY(curActionIndex);
                originX = locationX;
                originY = locationY;
                /*
                当ACTION_POINTER_UP动作过后index会重新更新，删除掉抬起手指的index，后面的index向前移动。
                所以index：0又表示为按下手指的索引了
                 */
                curActionIndex = 0;
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = event.getX(curActionIndex) - downX;
                float moveY = event.getY(curActionIndex) - downY;
                locationX = originX + moveX;
                locationY = originY + moveY;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                Log.e("onTouchEvent", "ACTION_UP: ");
                break;
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap, locationX, locationY, paint);
    }
}
